﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using WaterBill.DTO;
using System.Data.SqlClient;

namespace WaterBill.DAL
{
    class DAL_Customers : clsConnection
    {
        DataTable dtC;
        //Lay danh sach tat ca khach hang
        public DataTable DAL_Customers_GetAll()
        {
            dtC = new DataTable();
            try
            {
                dtC = ExcuteQuery("GetAllCustomer");
            }
            catch
            {
            }
            return dtC;
        }

        //Lay danh sach khach hang co trang thai bang 1
        public DataTable DAL_Customers_GetStatusEnable()
        {
            dtC = new DataTable();
            try
            {
                dtC = ExcuteQuery("GetCustomerStatusEnable");
            }
            catch
            {
            }
            return dtC;
        }

        //Lay danh sach khach hang chua co hoa don dua vao thang truyen vao
        public DataTable DAL_Customers_GetNotBillOfCharge(int ChargeId)
        {
            dtC = new DataTable();
            try
            {
                AddParameter(new SqlParameter("@ChargeId", ChargeId));
                dtC = ExcuteQuery("GetCustomerNotBillOfCharge");
            }
            catch
            {
            }
            return dtC;
        }

        //Them mot khach hang moi
        public Boolean DAL_Customers_Insert(DTO_Customers obj)
        {
            try
            {
                AddParameter(new SqlParameter("@FullName", obj.FullName.Trim()));
                AddParameter(new SqlParameter("@HouseNumber", obj.HouseNumber.Trim()));
                AddParameter(new SqlParameter("@NestId", obj.NestId));
                AddParameter(new SqlParameter("@Phone", obj.Phone.Trim()));
                AddParameter(new SqlParameter("@RegisteredDate", obj.RegisteredDate));
                AddParameter(new SqlParameter("@Status", obj.Status));
                if (ExcuteNonQuery("InsertCustomer"))
                {
                    return true;
                }
            }
            catch
            {
            }
            return false;
        }

        //Sua khach hang
        public Boolean DAL_Customers_Update(DTO_Customers obj)
        {
            try
            {
                AddParameter(new SqlParameter("@CustomerId", obj.CustomerId));
                AddParameter(new SqlParameter("@FullName", obj.FullName.Trim()));
                AddParameter(new SqlParameter("@HouseNumber", obj.HouseNumber.Trim()));
                AddParameter(new SqlParameter("@NestId", obj.NestId));
                AddParameter(new SqlParameter("@Phone", obj.Phone.Trim()));
                AddParameter(new SqlParameter("@Status", obj.Status));
                if (ExcuteNonQuery("UpdateCustomer"))
                {
                    return true;
                }
            }
            catch
            {
            }
            return false;
        }

        //Xoa khach hang
        public Boolean DAL_Customers_Delete(int CustomerId)
        {
            try
            {
                AddParameter(new SqlParameter("@CustomerId", CustomerId));
               
                if (ExecuteScalar("DeleteCustomer") == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }       
            }
            catch
            {
            }
            return false;
        }
    }
}
